<?php

class SiteController extends Controller
{

    /**
     * Declares class-based actions.
     */
    public function actions()
    {
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xFFFFFF,
            ),
            // page action renders "static" pages stored under 'protected/views/site/pages'
            // They can be accessed via: index.php?r=site/page&view=FileName
            'page' => array(
                'class' => 'CViewAction',
            ),
        );
    }

    /**
     * This is the default 'index' action that is invoked
     * when an action is not explicitly requested by users.
     */
    public function actionIndex()
    {
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'

        
        $criteria = new CDbCriteria();
        $criteria->select = '*, rand() as rand';
        $criteria->condition = 'type=:type';
        $criteria->params = array(':type' => Posts::POST_TYPE_PRODUCT);
        $criteria->limit = '18';
        $criteria->order = 'rand';

        $this->pageTitle = OpdSettings::getSetting('keywords') . ' | ' . Yii::app()->name;
        Yii::app()->clientScript->registerMetaTag(OpdSettings::getSetting('keywords') . ' | ' . Yii::app()->name, 'description');
//        Yii::app()->clientScript->registerMetaTag(OpdSettings::getSetting('keywords'), 'keywords');

        $model = Posts::model()->findAll($criteria);

        $this->render('index', array(
            'model' => $model
            )
        );
    }

    /**
     * This is the action to handle external exceptions.
     */
    public function actionError()
    {
        if ($error = Yii::app()->errorHandler->error)
        {
            if (Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }

    public function actionSendContact()
    {
        if (Yii::app()->request->isAjaxRequest)
        {
            $model = new ContactForm;
            if (isset($_POST['ContactForm']))
            {
                $model->attributes = $_POST['ContactForm'];
                if ($model->validate())
                {
                    $name = '=?UTF-8?B?' . base64_encode($model->name) . '?=';
                    $subject = '=?UTF-8?B?' . base64_encode($model->subject) . '?=';
                    $headers = "From: $name <{$model->email}>\r\n" .
                        "Reply-To: {$model->email}\r\n" .
                        "MIME-Version: 1.0\r\n" .
                        "Content-type: text/plain; charset=UTF-8";

                    mail(Yii::app()->params['adminEmail'], $subject, $model->body, $headers);

                    echo 'ok';
                }
                else
                {
                    echo CActiveForm::validate($model);
                }
            }
        }
    }

    /**
     * Displays the contact page
     */
    public function actionContact()
    {
        $model = new ContactForm;
        if (isset($_POST['ContactForm']))
        {
            $model->attributes = $_POST['ContactForm'];
            if ($model->validate())
            {
                $name = '=?UTF-8?B?' . base64_encode($model->name) . '?=';
                $subject = '=?UTF-8?B?' . base64_encode($model->subject) . '?=';
                $headers = "From: $name <{$model->email}>\r\n" .
                    "Reply-To: {$model->email}\r\n" .
                    "MIME-Version: 1.0\r\n" .
                    "Content-type: text/plain; charset=UTF-8";

                mail(Yii::app()->params['adminEmail'], $subject, $model->body, $headers);
                Yii::app()->user->setFlash('contact', 'Thank you for contacting us. We will respond to you as soon as possible.');
                $this->refresh();
            }
        }
        $this->pageTitle = 'Giới thiệu | ' . Yii::app()->name;
        Yii::app()->clientScript->registerMetaTag('Giới thiệu | ' . Yii::app()->name, 'description');
        $this->render('contact', array('model' => $model));
    }

    /**
     * Displays the login page
     */
    public function actionLogin()
    {
        $model = new LoginForm;

        // if it is ajax validation request
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        // collect user input data
        if (isset($_POST['LoginForm']))
        {
            $model->attributes = $_POST['LoginForm'];
            // validate user input and redirect to the previous page if valid
            if ($model->validate() && $model->login())
                $this->redirect(Yii::app()->user->returnUrl);
        }
        // display the login form
        $this->render('login', array('model' => $model));
    }

    /**
     * Logs out the current user and redirect to homepage.
     */
    public function actionLogout()
    {
        Yii::app()->user->logout();
        $this->redirect(Yii::app()->homeUrl);
    }

    public function actionWarranty()
    {
        $model = Posts::model()->find('type=' . Posts::POST_TYPE_WARRANTY);
        if ($model == Null)
        {
            $this->render('error');
            Yii::app()->end();
        }

        $this->pageTitle = 'DỊCH VỤ BẢO HÀNH - BẢO TRÌ   | ' . Yii::app()->name;
        Yii::app()->clientScript->registerMetaTag('DỊCH VỤ BẢO HÀNH - BẢO TRÌ   | ' . Yii::app()->name, 'description');
        $this->render('warranty', array(
            'model' => $model,
        ));
    }

    public function actionContactUs()
    {
        $this->pageTitle = 'Liên hệ | ' . Yii::app()->name;
        Yii::app()->clientScript->registerMetaTag('Liên hệ | ' . Yii::app()->name, 'description');
        $this->render('contactus');
    }

    public function actionAboutUs()
    {
        
//        $model = Posts::model()->find('type =:type',array(':type'=>Posts::POST_TYPE_ABOUTUS));
        $model = Posts::model()->find('type =0');
        
        
        if ($model == NULL)
        {
        
            $this->render('error');
            Yii::app()->end();
        }

        $this->pageTitle = 'Giới thiệu | ' . Yii::app()->name;
        Yii::app()->clientScript->registerMetaTag('Giới thiệu | ' . Yii::app()->name, 'description');
        $this->render('aboutus', array(
            'model' => $model,
        ));
    }

    public function actionNews()
    {

        $criteria = new CDbCriteria();
        $criteria->limit = 6;
        $criteria->condition = 'type=:type AND status=:status';
        $criteria->params = array(
            ':type' => Posts::POST_TYPE_NEWS, 
            ':status' => Posts::POST_STATUS_ENABLE);
        $criteria->order = 'post_id DESC';

        $count = Posts::model()->count($criteria);
        $pages = new CPagination($count);
        $pages->pageSize = 6;
        $pages->applyLimit($criteria);

        $model = Posts::model()->findAll($criteria);

        if ($model == NULL)
        {
            $this->render('error');
            Yii::app()->end();
        }

        $this->pageTitle = 'Tin tức  | ' . Yii::app()->name;
        Yii::app()->clientScript->registerMetaTag('Tin tức  | ' . Yii::app()->name, 'description');
        $this->render('news', array(
            'model' => $model,
            'pages' => $pages
        ));
    }

    public function actionProduct()
    {

        $this->pageTitle = 'Sản phẩm  | ' . Yii::app()->name;
        Yii::app()->clientScript->registerMetaTag(Yii::app()->name, 'description');

        $this->render('product_details', array());
    }

    public function actionVideo()
    {
        $criteria = new CDbCriteria();
        $criteria->limit = 6;
        $criteria->condition = 'type=:type AND status=:status';
        $criteria->params = array(
            ':type' => Posts::POST_TYPE_VIDEO, 
            ':status' => Posts::POST_STATUS_ENABLE);
        $criteria->order = 'post_id DESC';

        $count = Posts::model()->count($criteria);
        $pages = new CPagination($count);
        $pages->pageSize = 6;
        $pages->applyLimit($criteria);

        $this->pageTitle = 'Video  | ' . Yii::app()->name;
        Yii::app()->clientScript->registerMetaTag(Yii::app()->name, 'description');
        $model = Posts::model()->findAll($criteria) ;
        $this->render('video', array(
            'model' => $model,
            'pages' => $pages
        ));
    }

}